import request from '../request.ts'

// 用户管理
export const getUsers = () => {
  return request({
    url: '/rbac/users',
    method: 'get'
  })
}

export const getUser = (userId) => {
  return request({
    url: `/rbac/users/${userId}`,
    method: 'get'
  })
}

export const getUserByUsername = (username) => {
  return request({
    url: `/rbac/users/by-username/${username}`,
    method: 'get'
  })
}

export const createUser = (data) => {
  return request({
    url: '/rbac/users',
    method: 'post',
    data
  })
}

export const updateUser = (userId, data) => {
  return request({
    url: `/rbac/users/${userId}`,
    method: 'put',
    data
  })
}

export const deleteUser = (userId) => {
  return request({
    url: `/rbac/users/${userId}`,
    method: 'delete'
  })
}

// 角色管理
export const getRoles = () => {
  return request({
    url: '/rbac/roles',
    method: 'get'
  })
}

export const getRole = (roleId) => {
  return request({
    url: `/rbac/roles/${roleId}`,
    method: 'get'
  })
}

export const createRole = (data) => {
  return request({
    url: '/rbac/roles',
    method: 'post',
    data
  })
}

export const updateRole = (roleId, data) => {
  return request({
    url: `/rbac/roles/${roleId}`,
    method: 'put',
    data
  })
}

export const deleteRole = (roleId) => {
  return request({
    url: `/rbac/roles/${roleId}`,
    method: 'delete'
  })
}

// 权限管理
export const getPermissions = () => {
  return request({
    url: '/rbac/permissions',
    method: 'get'
  })
}

export const getPermission = (permissionId) => {
  return request({
    url: `/rbac/permissions/${permissionId}`,
    method: 'get'
  })
}

export const createPermission = (data) => {
  return request({
    url: '/rbac/permissions',
    method: 'post',
    data
  })
}

export const updatePermission = (permissionId, data) => {
  return request({
    url: `/rbac/permissions/${permissionId}`,
    method: 'put',
    data
  })
}

export const deletePermission = (permissionId) => {
  return request({
    url: `/rbac/permissions/${permissionId}`,
    method: 'delete'
  })
}

// 用户角色关联
export const assignRoleToUser = (userId, roleId) => {
  return request({
    url: `/rbac/users/${userId}/roles/${roleId}`,
    method: 'post'
  })
}

export const removeRoleFromUser = (userId, roleId) => {
  return request({
    url: `/rbac/users/${userId}/roles/${roleId}`,
    method: 'delete'
  })
}

export const getUserRoles = (userId) => {
  return request({
    url: `/rbac/users/${userId}/roles`,
    method: 'get'
  })
}

// 角色权限关联
export const assignPermissionToRole = (roleId, permissionId) => {
  return request({
    url: `/rbac/roles/${roleId}/permissions/${permissionId}`,
    method: 'post'
  })
}

export const removePermissionFromRole = (roleId, permissionId) => {
  return request({
    url: `/rbac/roles/${roleId}/permissions/${permissionId}`,
    method: 'delete'
  })
}

export const getRolePermissions = (roleId) => {
  return request({
    url: `/rbac/roles/${roleId}/permissions`,
    method: 'get'
  })
}

// 用户权限关联
export const assignPermissionToUser = (userId, permissionId) => {
  return request({
    url: `/rbac/users/${userId}/permissions/${permissionId}`,
    method: 'post'
  })
}

export const removePermissionFromUser = (userId, permissionId) => {
  return request({
    url: `/rbac/users/${userId}/permissions/${permissionId}`,
    method: 'delete'
  })
}

export const getUserPermissions = (userId) => {
  return request({
    url: `/rbac/users/${userId}/permissions`,
    method: 'get'
  })
}

// 创建教师用户
export const createTeacherUser = (data) => {
  return request({
    url: '/rbac/create-teacher',
    method: 'post',
    data
  })
}
